package com.example.demo.common;


import com.example.demo.util.StringUtil;

import java.io.Serializable;

/**
 * Created by cqh on 17-9-5.
 */
public class ResultObject<T> implements Serializable {

    long timestamp= System.currentTimeMillis();

    int status=200;

    String error="SUCCESS";

    String errorCode;

    String group;

    String exception;

    String message="处理成功";

    String path;

    /**
     * 请求成功状态码
     */
    public static final String SUCCESS = "SUCCESS";
    /**
     *  未授权（未登录）
     */
    public static final String NOT_OAUTH = "50000";

    /**
     * 无权限访问
     */
    public static final String PERMISSION_DENIED = "50001";

    /**
     * 非法请求
     */
    public static final String ILLEGAL_REQUEST = "50002";

    /**
     * 无效的请求参数
     */
    public static final String INVALID_PARAM = "50003";

    /**
     * 无效的签名
     */
    private static final String INVALID_SIGN = "50005";

    /**
     * 请求接口不存在
     */
    public static final String API_NOT_EXIST = "50004";

    /**
     * 无效请求方法
     */
    public static final String INVALID_REQUEST_METHOD = "50005";

    /**
     * 用户被锁定
     */
    public static final String ACCOUNT_LOCKED = "50006";

    /**
     * 系统异常
     */
    public static final String SYSTEM_ERROR = "50007";

    /**
     * 系统繁忙
     */
    public static final String SYSTEM_BUSY = "50008";

    /**
     * 普通异常
     */
    public static final String SYSTEM_COMMON_ERROR = "100";


    T data;

    public ResultObject(){
        this.error = "SUCCESS";
        this.errorCode = SUCCESS;
        this.message = "success";
    }

    public ResultObject(String error,String code, String msg){
        this.error = error;
        this.errorCode = code;
        this.message = msg;
    }

    public static <T> ResultObject<T> success(){
        return new ResultObject<>("SUCCESS",SUCCESS, "success");
    }

    public static <T> ResultObject<T> locked(){
        return new ResultObject<>(ACCOUNT_LOCKED,"ACCOUNT_LOCKED", "用户被锁定");
    }

    public static <T> ResultObject<T> unauthorized(){
        return new ResultObject<>(NOT_OAUTH, "NOT_OAUTH","请先登录！");
    }

    public static <T> ResultObject<T> permissionDenied(){
        return new ResultObject<>(PERMISSION_DENIED, "PERMISSION_DENIED","无访问权限");
    }

    public static <T> ResultObject<T> illagalRequest(){
        return new ResultObject<>(ILLEGAL_REQUEST, "ILLEGAL_REQUEST", "非法请求");
    }

    public static <T> ResultObject<T> invalidSign() {
        return new ResultObject<>(INVALID_SIGN, "INVALID_SIGN","无效的签名");
    }

    public static <T> ResultObject<T> invalidParam(){
        return new ResultObject<>(INVALID_PARAM,"INVALID_PARAM", "参数校验不通过");
    }

    public static <T> ResultObject<T> notFound(){
        return new ResultObject<>(API_NOT_EXIST, "API_NOT_EXIST","请求地址错误");
    }

    public static <T> ResultObject<T> systemError(){
        return new ResultObject<>(SYSTEM_ERROR, "SYSTEM_ERROR","服务器错误");
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getError() {
        return error;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setIenum(IEnum ienum){
        if(null!=ienum&&!StringUtil.isNullString(ienum.getName())) {
            this.error = ienum.getCode();
            this.message = ienum.getValue();
            this.errorCode = ienum.getName();
            this.group = ienum.getGroup();
        }
    }

    public void setErrorInfo(ResultObject resultObject) {
        this.error = resultObject.getError();
        this.message = resultObject.getMessage();
        this.group = resultObject.getGroup();
        this.errorCode = resultObject.getErrorCode();
    }

    public void setError(String error) {
        this.error = error;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
}

